/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmRegistrarPadronElectoral.java
 *
 * Created on 20/06/2011, 06:06:04 PM
 */
package verins.GUI;

import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import verins.BO.PadronElectoralBO;
import verins.BO.PersonaBO;
import verins.BO.PersonarnvBO;
import verins.Utils.Comunes;
import verins.Utils.Mensajes;
import verins.beans.Estadopadron;
import verins.beans.Estadoprocesoelectoral;
import verins.beans.Grupo;
import verins.beans.Padronelectoral;
import verins.beans.Partidopoliticoxprocesoelectoral;
import verins.beans.Persona;
import verins.beans.Personarnv;
import verins.beans.Procesoelectoral;
import verins.beans.Usuario;

/**
 *
 * @author jpenam
 */
public class frmRegistrarPadronElectoral extends JDialog {

    /** Creates new form frmRegistrarPadronElectoral */
    
     Procesoelectoral procesoEle=null;
     Partidopoliticoxprocesoelectoral parxpro=null;
     int cantidad=0;
    Usuario usuario = null;
    Persona persona=null;
    public frmRegistrarPadronElectoral(Procesoelectoral proceso, JFrame ventana, Usuario usuario) {
        procesoEle=proceso;
        this.usuario = usuario;
        
        initComponents();
        Comunes.centrarVentana(this);
        setValidadores();
        //cmbPartido.enable(false);
    }

    
    private void setValidadores(){
        txtCantidad.setDocument(new verins.Utils.LimitInput(3));
        txtCantidad.addKeyListener(verins.Utils.Verificador.verificaSoloNumeros());
        txtDNI.setDocument(new verins.Utils.LimitInput(8));
        txtDNI.addKeyListener(verins.Utils.Verificador.verificaSoloNumeros());
    }
   
    private  boolean  validarCampos(){
    boolean valido=true;
    
        if (procesoEle==null){
            verins.Utils.Mensajes.alerta("Debe elegir un proceso electoral");
            return false;
        }
        if (parxpro==null){
            verins.Utils.Mensajes.alerta("Debe elegir un partido");
            return false;
        }
        if (txtCantidad.getText().trim().length()==0){
            Mensajes.alerta("Debe ingresar una cantidad de padrones a asignar");
            return false;
        }
        
        if (persona==null){
            Mensajes.alerta("Encargado de padrón no encontrado");
            return false;
        }
    return valido;
    }
    
    
   public void setProcesoElec(Procesoelectoral proceso){
       procesoEle=proceso;
       txtProcesoElec.setText(procesoEle.getNombre());
   }
   
   
   private void obtenerCampos(){
       parxpro=(Partidopoliticoxprocesoelectoral)cmbPartido.getSelectedItem();
       if (txtCantidad.getText().trim().length()>0)
            cantidad = Integer.parseInt(txtCantidad.getText());
   }
   
    
   
   @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jpPlant01 = new javax.swing.JPanel();
        cmbPartido = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtProcesoElec = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnBusq = new javax.swing.JButton();
        txtCantidad = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtDNI = new javax.swing.JTextField();
        btnBusPers = new javax.swing.JButton();
        btnAsignar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Asignar padrones electorales");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Generales"));

        javax.swing.GroupLayout jpPlant01Layout = new javax.swing.GroupLayout(jpPlant01);
        jpPlant01.setLayout(jpPlant01Layout);
        jpPlant01Layout.setHorizontalGroup(
            jpPlant01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jpPlant01Layout.setVerticalGroup(
            jpPlant01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 88, Short.MAX_VALUE)
        );

        jLabel3.setText("Partido político");

        txtProcesoElec.setEnabled(false);

        jLabel2.setText("Proceso electoral");

        btnBusq.setText("...");
        btnBusq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBusqActionPerformed(evt);
            }
        });

        jLabel4.setText("Cantidad de Padrones");

        jLabel5.setText("Responsable");

        txtDNI.setEnabled(false);

        btnBusPers.setText("...");
        btnBusPers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBusPersActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jpPlant01, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtDNI, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnBusPers, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(398, 398, 398))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtProcesoElec, javax.swing.GroupLayout.PREFERRED_SIZE, 487, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                                .addComponent(btnBusq, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(29, 29, 29))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cmbPartido, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(292, Short.MAX_VALUE))))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtProcesoElec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBusq)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbPartido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpPlant01, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtDNI, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnBusPers))))
                .addContainerGap())
        );

        btnAsignar.setText("Aceptar");
        btnAsignar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAsignarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAsignar, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnAsignar))
                .addGap(17, 17, 17))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBusqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBusqActionPerformed
        // TODO add your handling code here:
        frmBusquedaProcesoElectoral frm=new frmBusquedaProcesoElectoral(usuario, null);
        frm.setearBotonesBusqueda();
        frm.setearEstadoBusq(new Estadoprocesoelectoral(1));
        frm.setVisible(true);
        procesoEle=frm.proceso;
        if (procesoEle!=null){
            procesoEle=frm.proceso;
            setProcesoElec(procesoEle);
            actualizarComboPartidos();
        }
}//GEN-LAST:event_btnBusqActionPerformed

    private void btnAsignarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAsignarActionPerformed
        // TODO add your handling code here:
        boolean rOk=true;
        obtenerCampos();
        if (validarCampos()){
            PersonaBO.save(persona);
            int lastid=2;
            String nuevoCod="";
            int nuevoCodInt=0;
            Padronelectoral Padron=null;
            for(int i=0;i<cantidad;i++){
                nuevoCod=PadronElectoralBO.getNuevoCodigo();
                Padron = new Padronelectoral ();
                Padron.setEstado(new Estadopadron(1));
                Padron.setIdpadronelectoral(nuevoCod);
                nuevoCodInt=Integer.parseInt(nuevoCod.substring(1, nuevoCod.length()));
                Padron.setId(nuevoCodInt);
                Padron.setIdgrupo(new Grupo(1));
                Padron.setIdpartidopolitico(parxpro.getPartidopolitico());
                Padron.setIdresponsable(persona);  
                Padron.setIdprocesoelectoral(procesoEle.getIdprocesoelectoral());
                rOk=rOk && PadronElectoralBO.save(Padron,usuario);
            }
            Mensajes.exito(rOk);
            if (rOk) dispose();
        }
    }//GEN-LAST:event_btnAsignarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnBusPersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBusPersActionPerformed
        // TODO add your handling code here:
        frmBusquedaPersona frm=new frmBusquedaPersona(this);
        frm.mostrarBotonAgregar(true);
        frm.setVisible(true);
        Personarnv per=(Personarnv)frm.personaBusqueda;
        
        if (per!=null){
            persona = Comunes.convertirAPersona(per);
            txtDNI.setText(persona.getDni());
        }
    }//GEN-LAST:event_btnBusPersActionPerformed

    private void  actualizarComboPartidos(){
        cmbPartido.removeAllItems();
        List lst=procesoEle.getPartidopoliticoxprocesoelectoralList();
        Comunes.llenarCombo(cmbPartido, lst);
       
      
    }
    
    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAsignar;
    private javax.swing.JButton btnBusPers;
    private javax.swing.JButton btnBusq;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cmbPartido;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jpPlant01;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtDNI;
    private javax.swing.JTextField txtProcesoElec;
    // End of variables declaration//GEN-END:variables
}
